package com.warski.wonderfires.page.music;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.warski.wonderfires.R;
import com.warski.wonderfires.model.MusicItemInfo;
import com.warski.wonderfires.model.SongListItemInfo;
import com.warski.wonderfires.utils.FileUtils;
import com.warski.wonderfires.utils.Logger;
import com.warski.wonderfires.utils.StringUtils;
import com.warski.wonderfires.utils.json.JsonUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuhaiping on 2020/8/29.
 * 歌单数据
 */
public class SongListAdaopter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context context;
    public List<SongListItemInfo> songsList = new ArrayList<SongListItemInfo>();

    public SongListAdaopter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        //测试数据，后期应该从网络或者服务器获取歌单信息
        loadTestData();
    }

    @Override
    public int getCount() {
        return songsList.get(0).getMusicList().size();
    }

    @Override
    public Object getItem(int position) {
        return songsList.get(0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.song_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        MusicItemInfo musicItemInfo = songsList.get(0).getMusicList().get(position);

        viewHolder.songName.setText(musicItemInfo.getName());

        //将来这里可以补上其他情况，比如网络图片

        viewHolder.songAuthor.setText(musicItemInfo.getAuthor());

        return convertView;
    }

    class ViewHolder {
        public TextView songName;
        public TextView songAuthor;
        public ViewHolder(View view) {
            songName = view.findViewById(R.id.song_name);
            songAuthor =  view.findViewById(R.id.song_author);
        }
    }

    /**
     * 加载本地测试歌单
     * 要求：xxx.json(描述文件), xxx.jpg(图片), xxx文件夹(歌曲文件夹)
     */
    private void loadTestData() {
        try {
            String[] files = FileUtils.getAssetsFiles("");
            for (String file : files) {
                if (file.endsWith(".json")) {
                    //歌单描述文件
                    String songListFileName = file.substring(0, file.length() - 5);
                    String songListJson = FileUtils.readAssetAsText(file);
                    if (StringUtils.isNotEmpty(songListJson)) {
                        try {
                            SongListItemInfo songListItemInfo = JsonUtils.fromJson(songListJson, SongListItemInfo.class);
                            songListItemInfo.setImage(String.format("ASSET:%s.jpg", songListFileName));
                            //读取歌曲
                            try {
                                String[] musicfiles = FileUtils.getAssetsFiles(songListFileName);
                                for (String musicfile : musicfiles) {
                                    if (musicfile.endsWith(".mp3")) {
                                        String musicFileName = musicfile.substring(0, musicfile.length() - 4);
                                        MusicItemInfo musicItemInfo = new MusicItemInfo();
                                        musicItemInfo.setName(musicFileName.substring(musicFileName.indexOf(" - ") + 3));
                                        musicItemInfo.setAuthor(musicFileName.substring(0, musicFileName.indexOf(" - ")));

                                        String lrcPath = String.format("ASSET:%s/%s.lrc", songListFileName, musicFileName);
                                        boolean haveFile = FileUtils.haveFile(lrcPath);
                                        musicItemInfo.setLrc(haveFile?lrcPath:"");
                                        Log.i("have","haveFile--->" + haveFile + "   lrcPath:"+musicItemInfo.getLrc());
                                        musicItemInfo.setImage(String.format("ASSET:%s/%s.jpg", songListFileName, musicFileName));
                                        musicItemInfo.setUrl(String.format("ASSET:%s/%s", songListFileName, musicfile));
                                        musicItemInfo.setTime(0);//本地文件不获取时长
                                        songListItemInfo.addMusic(musicItemInfo);
                                    }
                                }
                            } catch (Exception e) {
                                Logger.error("读取歌曲失败");
                                Logger.error(e);
                            }

                            songsList.add(songListItemInfo);
                        } catch (Exception e) {
                            Logger.error("解析歌单失败");
                            Logger.error(e);
                        }
                    }
                }
            }

        } catch (Exception e) {
            Logger.error("访问Asset失败");
            Logger.error(e);
        }

        Logger.info("本地测试歌单数据");
        Logger.info(JsonUtils.toJson(songsList));
    }
}
